/******************************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Ltd.                       */
/*                                                                            */
/*  NAME      = ibmmq_init.c                                                  */
/*  NOTE      =                                                               */
/*  DATE      = 2016/06/03 by wuwk                                            */
/******************************************************************************/

#include <stdio.h>

#include "frcomm.h"
#include "frmem.h"
#include "frlog.h"

#include "conntrack.h"
#include "module.h"

#include "ibmmq_core.h"
#include "config.h"
#include "cnaps2.h"

//框架的上下文引用
void *g_p_apm_context=NULL;

pthread_spinlock_t g_splock_cnaps2_stmt;
//存储二代支付的请求与响应
Frhash_header *g_p_hash_cnaps2_stmt = NULL;

#ifdef  __cplusplus
extern "C"
{
#endif
/*******************************************************************************
*  FUNC     :  ibmmq初始化
*  ARGS     :  p_context                    框架的上下文引用
*           :  p_module_params              模块入力参数
*           :  p_module_cfg                 模块配置信息出力
*  RTN      :  FR_FAIL
*           :  FR_SUCC
*  NOTE     :
*******************************************************************************/
int module_init(void *p_context, const char* p_module_params, L7_module_config *p_module_cfg)
{
    if(p_context == NULL || p_module_params == NULL || p_module_cfg == NULL)
        return FR_FAIL;

    g_p_apm_context = p_context;
    if( fr_slab_init( (Frmem_env *)(((ApmContext *)g_p_apm_context)->p_st_mem_env), MODULE_NAME) == NULL )
    {
        return FR_FAIL;
    }

    frlog_set_hd(((ApmContext *)g_p_apm_context)->p_st_log_category);
    p_module_cfg->p_l4_proto = MODULE_L4PROTO;
    p_module_cfg->i_l7_proto = MODULE_L7PROTO;
    p_module_cfg->p_module_name = MODULE_NAME;
    p_module_cfg->p_version = MODULE_VERSION " " MODULE_TIME;
    p_module_cfg->l7_detect = ibmmq_detect;
    p_module_cfg->l7_analysis = ibmmq_analysis;
    p_module_cfg->session_destory = ibmmq_session_destroy;

    pthread_spin_init(&g_splock_cnaps2_stmt, 0);
    if ((g_p_hash_cnaps2_stmt = frhash2_create2(APM_CNAPS2_HASH_SIZE)) == NULL)
        return FR_FAIL;

    cnaps2_mesg_type_quicksort(0, -1); // 对arr_mesg_type_maps进行快速排序
    cnaps2_mesg_resp_quicksort(0, -1); // 对arr_mesg_resp进行快速排序

    return FR_SUCC;
}

/*******************************************************************************
*  FUNC     :  ibmmq销毁
*  ARGS     :  void
*  RTN      :
*  NOTE     :
*******************************************************************************/
void module_exit(void)
{
    frdbg("ibmmq_destroy\n");

    frhash2_free2(g_p_hash_cnaps2_stmt, cnaps2_stmt_node_free);
    pthread_spin_destroy(&g_splock_cnaps2_stmt);

    fr_slab_destroy();

    return;
}

#ifdef  __cplusplus
}
#endif
